package com.walleipt;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.bytecode.Proxy;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.proxy.InvokerInvocationHandler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;

public class Proxy_Main {

    public static void main(String[] args) {

        // InvocationHandler
        ISayHiServices test = (ISayHiServices) Proxy.getProxy(ISayHiServices.class).newInstance(new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.err.println( proxy );
                for ( Object arg : args ) {
                    System.err.println( arg );
                }
                
                return null;
            }
        });
        test.sayHi("程","辉");

        // InvokerInvocationHandler
        ISayHiServices test1 = (ISayHiServices) Proxy.getProxy(ISayHiServices.class).newInstance( new InvokerInvocationHandler(new Invoker<String>() {
            @Override
            public Class<String> getInterface() {
                return String.class;
            }

            @Override
            public Result invoke(Invocation invocation) throws RpcException {
                System.err.println( "invoaction=>" + invocation );
                return new Result() {
                    @Override
                    public Object getValue() {
                        return null;
                    }

                    @Override
                    public Throwable getException() {
                        return null;
                    }

                    @Override
                    public boolean hasException() {
                        return false;
                    }

                    @Override
                    public Object recreate() throws Throwable {
                        return null;
                    }

                    @Override
                    public Object getResult() {
                        return null;
                    }

                    @Override
                    public Map<String, String> getAttachments() {
                        return null;
                    }

                    @Override
                    public void addAttachments(Map<String, String> map) {

                    }

                    @Override
                    public void setAttachments(Map<String, String> map) {

                    }

                    @Override
                    public String getAttachment(String key) {
                        return null;
                    }

                    @Override
                    public String getAttachment(String key, String defaultValue) {
                        return null;
                    }

                    @Override
                    public void setAttachment(String key, String value) {

                    }
                };
            }

            @Override
            public URL getUrl() {
                return URL.valueOf( "itest://" );
            }

            @Override
            public boolean isAvailable() {
                return false;
            }

            @Override
            public void destroy() {

            }
        })) ;
        test1.sayHi( "Cheng", "Hui");
    }

    public static interface ISayHiServices {

        void sayHi(String name, String name2);
    }
}
